package model;

import control.InFile;
import control.PrintFile;

/**
 * Patente
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 *
 */
public class Patente {

	private  String  patNum = "";
	private  String  title = "";
	private  String  pd = ""; // privateation date
	private  String  inventor = ""; // inventor(es)
	private  String  applicant = ""; // depositante
	private  String  ipc = ""; // classificacao internacional
	private  String  application = ""; // numero de deposito
	private  String  priority = ""; // prioridade(s)
	private  String  family = "";
	private  String  familia = "";
	private  String  cited = "";
	private  String  citing = "";
	private  String  resumo = "";
	private  String  absTract = "";
	private  String  claims = "";
	private  String  description = "";
	private  String  figura = "";// filename of frontpage figure

	/**
	 * Construtor vazio Patente
	 */
	public Patente() {
		this.patNum = "";
		this.title = " ";
		this.absTract = " ";
		this.description = " ";
		this.claims = " ";
		this.family = "";
		this.figura = "";
	}


	/**
	 * Construtor Patente
	 * @param title titulo
	 * @param absTract resumo em ingles 
	 * @param description descricao
	 * @param claims objetivos
	 */
	public Patente(String title, String absTract, String description, String claims) {
		this.title = title;
		this.absTract = absTract;
		this.description = description;
		this.claims = claims;
	}
	

	/**
	 * getElementByName
	 * le conteudo xml da etiqueta tag
	 * 
	 * @param xml xml
	 * @param tag tag
	 * @return conteudo do tag do xml
	 */
	public String getElementByName(String xml, String tag) {
		System.out.println("Inicio do Patente -> getElementByName ");
		
		int	   beginIndex;
		int    endIndex;
		String resultado  ;
		
		// acessa String xml
		String tagOpen = "<" + tag + ">";
		String tagEnd = "</" + tag + ">";

		System.out.println("tagOpen" + " tagEnd");
		System.out.flush();
		
		// checa se as tags existem
		if (xml.contains(tagOpen) && xml.contains(tagEnd)) {
			beginIndex = xml.indexOf(tagOpen);
			endIndex = xml.indexOf(tagEnd);
			String content = xml.substring(beginIndex + tagOpen.length() + 1,
					endIndex - 1);
			System.out.println("Fim do Patente -> getElementByName ");
			resultado = content;
			return resultado;
		} else {
			System.err.println(tagOpen + " nao existe");
			System.err.println(tagEnd + " nao existe");
			System.exit(1);
			System.out.println("Fim do Patente -> getElementByName ");
			return null;
		}
	}


	/**
	 * exportXML
	 * @param pathname pathname
	 * @param patente patente
	 */
	public void exportXML(String pathname, Patente patente) {	
		System.out.println("Inicio do Patente -> exportXML ");
		
		// Abra arquivo Patente.xml
		String patXML = new InFile().readFile("patent.xml");
		
		// String[] de lugares comuns
		String[] lugarComum = { "_PN", "_TITLE", "_PD", "_IN", "_PA", "_IPC",
				"_AP", "_PR", "_FAMILIA", "_CITED", "_CITING", "_AB",
				"_CLAIMS", "_DESC", "_FIG" };
		
		// Fazer substituicoes
		patXML = patXML.replace(lugarComum[0], patente.patNum);
		patXML = patXML.replace(lugarComum[1], patente.title);
		patXML = patXML.replace(lugarComum[2], patente.pd);
		patXML = patXML.replace(lugarComum[3], patente.inventor);
		patXML = patXML.replace(lugarComum[4], patente.applicant);
		patXML = patXML.replace(lugarComum[5], patente.ipc);
		patXML = patXML.replace(lugarComum[6], patente.application);
		patXML = patXML.replace(lugarComum[7], patente.priority);
		patXML = patXML.replace(lugarComum[8], patente.familia);
		patXML = patXML.replace(lugarComum[9], patente.cited);
		patXML = patXML.replace(lugarComum[10], patente.citing);
		patXML = patXML.replace(lugarComum[11], patente.resumo);
		patXML = patXML.replace(lugarComum[12], patente.claims);
		patXML = patXML.replace(lugarComum[13], patente.description);
		// patXML = patXML.replace(lugarComum[14], patente.figura);

		// Criar arquivo patente.patNum.xml		
		new PrintFile(pathname, patXML);

		System.out.println("Fim do Patent -> exportXML ");
	}


	

	/**
	 * geraPatente
	 * @param patente patente
	 */
	public void geraPatente(Patente patente) {
		System.out.println("Inicio do Patente -> geraPatentee ");
		
		String[] abs = patente.absTract.split("\n");
		String queTipo = "";
		// switches
		for (int i = 0; i < abs.length; i++) {			
			if (abs[i].contains("Title:")) {
				queTipo = "title";
				i++;
			}
			if (abs[i].contains("date:")) {
				queTipo = "date";
				i++;
			}
			if (abs[i].contains("Inventor:")) {
				queTipo = "inventor";
				i++;
			}
			if (abs[i].contains("Applicant:")) {
				queTipo = "applicant";
				i++;
			}
			if (abs[i].contains("Classification:")) {
				queTipo = "ipc";
				i++;
			}
			if (abs[i].contains("international:")) {
				continue;
			}
			if (abs[i].contains("European:")) {
				continue;
			}
			if (abs[i].contains("number:")) {
				queTipo = "application";
				i++;
			}
			if (abs[i].contains("Priority number(s):")) {
				queTipo = "priority";
				i++;
			}
			if (abs[i].contains("Also published as:")) {
				queTipo = "familia";
				i++;
			}
			if (abs[i].contains("Cited documents:")) {
				queTipo = "cited";
				i++;
			}

			if (abs[i].contains("Abstract of")) {
				queTipo = "abstract";
				i++;
			}
			
			// entra dados patentarios
			if (queTipo.equals("title")) {
				patente.title = abs[i].toString();
				continue;
			}
			if (queTipo.equals("date")) {
				patente.pd = abs[i].toString();
				continue;
			}
			if (queTipo.equals("inventor")) {
				patente.inventor = patente.inventor.concat(abs[i].toString());
				continue;
			}
			if (queTipo.equals("applicant")) {
				patente.applicant = patente.applicant.concat(abs[i].toString());
				continue;
			}
			if (queTipo.equals("ipc")) {
				patente.ipc = patente.ipc.concat(abs[i].toString());
				continue;
			}
			if (queTipo.equals("application")) {
				patente.application = patente.application.concat(abs[i]
						.toString());
				continue;
			}
			if (queTipo.equals("priority")) {
				patente.priority = patente.priority.concat(abs[i].toString());
				continue;
			}
			if (queTipo.equals("familia")) {
				// patente.family=patente.family.concat( abs[i].toString());
				continue;
			}
			if (queTipo.equals("cited")) {
				patente.cited = patente.cited.concat(abs[i].toString());
				continue;
			}
			if (queTipo.equals("abstract")) {
				patente.resumo = patente.absTract.concat(abs[i].toString());
				continue;
			}
			System.out.println("Fim do patente -> geraPatentee ");
		}
	}
	
	
	
	
	
	

	/**
	 * getPatNum
	 * @return patNum
	 */
	public String getPatNum() {
		return patNum;
	}

	/**
	 * setPatNum
	 * @param patNum patNum
	 */
	public void setPatNum(String patNum) {
		this.patNum = patNum;
	}

	/**
	 * getTitle
	 * @return title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * setTitle
	 * @param title
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * getPd
	 * @return pd
	 */
	public String getPd() {
		return pd;
	}

	/**
	 * setPd
	 * @param pd
	 */
	public void setPd(String pd) {
		this.pd = pd;
	}

	/**
	 * getInventor
	 * @return inventor
	 */
	public String getInventor() {
		return inventor;
	}

	/**
	 * setInventor
	 * @param inventor
	 */
	public void setInventor(String inventor) {
		this.inventor = inventor;
	}

	/**
	 * getApplicant
	 * @return applicant
	 */
	public String getApplicant() {
		return applicant;
	}

	
	/**
	 * setApplicant
	 * @param applicant
	 */
	public void setApplicant(String applicant) {
		this.applicant = applicant;
	}

	/**
	 * getIpc
	 * @return ipc
	 */
	public String getIpc() {
		return ipc;
	}

	/**
	 * setIpc
	 * @param ipc
	 */
	public void setIpc(String ipc) {
		this.ipc = ipc;
	}

	/**
	 * getApplication
	 * @return application
	 */
	public String getApplication() {
		return application;
	}

	/**
	 * setApplication
	 * @param application
	 */
	public void setApplication(String application) {
		this.application = application;
	}

	/**
	 * getPriority
	 * @return priority
	 */
	public String getPriority() {
		return priority;
	}

	/**
	 * setPriority
	 * @param priority
	 */
	public void setPriority(String priority) {
		this.priority = priority;
	}

	/**
	 * getFamily
	 * @return family
	 */
	public String getFamily() {
		return family;
	}

	/**
	 * setFamily
	 * @param family
	 */
	public void setFamily(String family) {
		this.family = family;
	}

	/**
	 * getFamilia
	 * @return familia
	 */
	public String getFamilia() {
		return familia;
	}

	/**
	 * setFamilia
	 * @param familia
	 */
	public void setFamilia(String familia) {
		this.familia = familia;
	}

	
	/**
	 * getCited
	 * @return cited
	 */
	public String getCited() {
		return cited;
	}

	/**
	 * setCited
	 * @param cited
	 */
	public void setCited(String cited) {
		this.cited = cited;
	}

	/**
	 * getCiting
	 * @return citing
	 */
	public String getCiting() {
		return citing;
	}

	/**
	 * setCiting
	 * @param citing
	 */
	public void setCiting(String citing) {
		this.citing = citing;
	}

	/**
	 * getResumo
	 * @return resumo
	 */
	public String getResumo() {
		return resumo;
	}

	/**
	 * setResumo
	 * @param resumo
	 */
	public void setResumo(String resumo) {
		this.resumo = resumo;
	}

	
	/**
	 * getAbsTract
	 * @return absTract
	 */
	public String getAbsTract() {
		return absTract;
	}

	
	/**
	 * setAbsTract
	 * @param absTract
	 */
	public void setAbsTract(String absTract) {
		this.absTract = absTract;
	}

	/**
	 * getClaims
	 * @return claims
	 */
	public String getClaims() {
		return claims;
	}

	/**
	 * setClaims
	 * @param claims
	 */
	public void setClaims(String claims) {
		this.claims = claims;
	}

	/**
	 * getDescription
	 * @return description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * setDescription
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * getFigura
	 * @return figura
	 */
	public String getFigura() {
		return figura;
	}

	/**
	 * setFigura
	 * @param figura
	 */
	public void setFigura(String figura) {
		this.figura = figura;
	}


}
